{template 'tpl_header'}
{if !empty($orders) }
{loop $orders $order}
<div class="order-list_row">
    <div class="order-payment">
        <div class="order-payment_time">
            预订日期：{php echo date('Y-m-d', $order['created_at'])}
        </div>
        <div class="order-payment_status">
            {if $order['status'] == '1'}
            已支付
            {else}
            未支付
            {/if}
        </div>
        <div class="clear"></div>
    </div>
    <div class="order-payment_info" onclick="detail('{$type}',{$order['id']});">
        <div class="order-payment_img">
            <img src="{php echo tomedia($order['thumb'])}" alt="">
        </div>
        <div class="order-payment_detail">
            <div class="order-payment_title">
                {$order['name']}
            </div>
            <div class="order-payment_price">
                {$order['total_price']}
            </div>
            {if $type != 'insurance' }
            <div class="order-payment_start_time">
                出行日期：{php echo date('Y-m-d', $order['departure_date'])}
            </div>
            {else}
            <div class="order-payment_start_time">
                出行日期：{php echo date('Y-m-d', $order['start_day'])}
            </div>
            <div class="order-payment_start_time">
                返回日期：{php echo date('Y-m-d', $order['finish_day'])}
            </div>
            {/if}
        </div>
        <div class="clear"></div>
    </div>
    {if $order['status'] != '1'}
    <div class="order-payment_btn">
        <a onclick="del_order({$order['id']})" class="weui-btn weui-btn_mini weui-btn_default">取消</a>
        <a onclick="pay_order({$order['id']})" class="weui-btn weui-btn_mini weui-btn_primary"
           style="margin-bottom: 15px;">去支付</a>
    </div>
    {/if}
</div>
{/loop}
{else}
<div id="list_null" class="weui-loadmore weui-loadmore_line">
    <span class="weui-loadmore__tips">暂无数据</span>
</div>
{/if}
<div id="loading" class="weui-loadmore">
    <i class="weui-loading"></i>
    <span class="weui-loadmore__tips">正在加载</span>
</div>
{template 'tpl_footer'}
<script type="text/javascript" src="{$_W['siteroot']}app/resource/js/lib/mui.min.js"></script>
<script type="text/javascript" src="{$_W['siteroot']}app/resource/js/app/util.js"></script>
<script>
    $('#loading').hide();
    var page = 2;
    var is_next = '{$is_next}';
    var type = "{$type}";
    //设置图的高度
    $('.order-payment_img').height($('.order-payment_info').height() - 10);
    $('.order-payment_img img').height($('.order-payment_info').height() - 10);
    $(function () {
        $(document).scroll(function () {
            //当滚动条到底部时请求数据
            if ($('body').scrollHeight() - $('body').height() - 3 <= $(window).scrollTop()) {
                if (is_next == '1') {
                    //防止重复请求
                    is_next = '0';
                    $('#loading').show();
                    var data = {
                        'action': 'order_list',
                        'page': page,
                        'type': type
                    };
                    $.ajax({
                        url: "{php echo $this->createMobileUrl('ajax')}",
                        type: 'post',
                        data: data,
                        dataType: 'json',
                        success: function (res) {
                            if (res.status == 1) {
                                is_next = (res.data.is_next) ? '1' : '0';
                                console.log(res.data.list);
                                $('#loading').hide();
                                page++;
                                add_order(res.data.list);
                            } else {
                                $.toast(res.msg, "cancel");
                            }

                        },
                        error: function (res) {
                            $.toast('请求失败，请检测网络或联系客服', "cancel");
                        }
                    });
                }
            }
        });
    });

    function add_order(list) {
        var html = '';
        for (li in list) {
            html += '<div class="order-list_row">\n' +
                '    <div class="order-payment">\n' +
                '        <div class="order-payment_time">\n' +
                '            预订日期：' + list[li].created_at +
                '        </div>\n' +
                '        <div class="order-payment_status">\n' + list[li].status_str +
                '        </div>\n' +
                '        <div class="clear"></div>\n' +
                '    </div>\n' +
                '    <div class="order-payment_info" onclick="detail(' + type + ',' + list[li].id + '});">\n' +
                '        <div class="order-payment_img">\n' +
                '            <img src="' + list[li].thumb + '" alt="">\n' +
                '        </div>\n' +
                '        <div class="order-payment_detail">\n' +
                '            <div class="order-payment_title">\n' +
                '                ' + list[li].name + '\n' +
                '            </div>\n' +
                '            <div class="order-payment_price">\n' +
                '                ' + list[li].total_price + '\n' +
                '            </div>\n';
            if (type != 'insurance') {
                html += '            <div class="order-payment_start_time">\n' +
                    '                出行日期：' + list[li].departure_date + '\n' +
                    '            </div>\n';
            } else {
                html += '<div class="order-payment_start_time">\n' +
                    '                出行日期：' + list[li].start_day + '\n' +
                    '            </div>\n' +
                    '            <div class="order-payment_start_time">\n' +
                    '                返回日期：' + list[li].finish_day + '\n' +
                    '            </div>\n';
            }

            html += '</div>\n' +
                '        <div class="clear"></div>\n' +
                '    </div>\n';
            if (list[li].status != '1') {
                html += '    <div class="order-payment_btn">\n' +
                    '        <a onclick="del_order(' + list[li].id + ');" class="weui-btn weui-btn_mini weui-btn_default">取消</a>\n' +
                    '        <a onclick="pay_order(' + list[li].id + ');" class="weui-btn weui-btn_mini weui-btn_primary" style="margin-bottom: 15px;">去支付</a>\n' +
                    '    </div>\n';
            }

            html += '</div>';
        }
        $('#loading').before(html);
    }

    function detail(type, id) {
        var url = "";
        if (type != 'insurance') {
            url = "{php echo url('entry', array('m' => 'vj_bontrip','do' => 'order','op' => 'detail','type' => $type),true); }";
        } else {
            url = "{php echo url('entry', array('m' => 'vj_bontrip','do' => 'insurance','op' => 'payment'),true); }";
        }
        url += "&id=" + id;
        window.location.href = url;
    }

    function pay_order(id) {
        var is_pay_look = false;
        if (!is_pay_look) {
            is_pay_look = true;
            var data = {
                'action': 'get_order_detail',
                'id': id,
                'type': '{$type}'
            };
            $.ajax({
                url: "{php echo $this->createMobileUrl('ajax')}",
                type: 'post',
                data: data,
                dataType: 'json',
                success: function (res) {
                    if (res.status == 1) {
                        var data = res.data;
                        util.pay({
                            orderFee: data.total_price,
                            payMethod: 'wechat',
                            orderTitle: data.name,
                            orderTid: data.order_no,
                            module: 'vj_bontrip',
                            type: '{$type}',
                            success: function (result) {
                                $.toast('支付成功');
                                location.reload();
                            },
                            fail: function (result) {
                                $.toast(result.message, "cancel");
                                is_pay_look = false;
                            },
                            complete: function (result) {
                            }
                        });

                    } else {
                        is_pay_look = false;
                        $.toast(res.msg, "cancel");
                    }

                },
                error: function (res) {
                    is_pay_look = false;
                    $.toast('请求失败，请检测网络或联系客服', "cancel");
                }
            });
        }
    }

    function del_order(id) {
        $.confirm({
            title: '是否删除？',
            text: '删除之后无法恢复，是否删除？',
            onOK: function () {
                //点击确认
                var is_pay_look = false;
                if (!is_pay_look) {
                    is_pay_look = true;
                    var data = {
                        'action': 'del_order',
                        'id': id,
                        'type': '{$type}'
                    };
                    $.ajax({
                        url: "{php echo $this->createMobileUrl('ajax')}",
                        type: 'post',
                        data: data,
                        dataType: 'json',
                        success: function (res) {
                            if (res.status == 1) {
                                location.reload();
                            } else {
                                is_pay_look = false;
                                $.toast(res.msg, "cancel");
                            }

                        },
                        error: function (res) {
                            is_pay_look = false;
                            $.toast('请求失败，请检测网络或联系客服', "cancel");
                        }
                    });
                }
            },
            onCancel: function () {
            }
        });
    }

</script>
